import os
import time

from selenium import webdriver
from selenium.webdriver.common.by import By


class Register:
    def __init__(self):
        # 打开Chrome浏览器
        self.driver = webdriver.Chrome()
        self.driver.maximize_window()
        # 打开链接
        self.link = 'file:///' + os.path.abspath('注册实例.html')
        self.driver.get(self.link)

    def example(self):
        # 定位用户名输入框,输入用户名
        username = self.driver.find_element(By.ID, 'user')
        username.send_keys('123456')
        time.sleep(2)
        # 定位密码输入框,输入密码
        password = self.driver.find_element(By.ID, 'password')
        password.send_keys('qweqwe22')
        time.sleep(2)
        # 定位电话号码输入框,输入电话号码
        tel = self.driver.find_element(By.ID, 'tel')
        tel.send_keys('13277968935')
        time.sleep(2)
        # 定位电子邮箱输入框,输入电子邮箱
        e_mail = self.driver.find_element(By.ID, 'email')
        e_mail.send_keys('525855722@qq.com')
        time.sleep(2)
        # 点击注册
        self.driver.find_element(By.CSS_SELECTOR, 'button').click()

    def exampleA(self):
        # 切换至注册A
        A = self.driver.find_element(By.ID, 'idframe1')
        self.driver.switch_to.frame(A)
        # 定位账号输入框输入账号
        time.sleep(2)
        username = self.driver.find_element(By.ID, 'userA')
        username.send_keys('222222222')
        # 定位密码输入框输入密码
        time.sleep(2)
        password = self.driver.find_element(By.ID, 'passwordA')
        password.send_keys('12345678')
        # 定位电话号码输入框,输入电话号码
        time.sleep(2)
        tel = self.driver.find_element(By.ID, 'telA')
        tel.send_keys('13277968935')
        # 定位电子邮箱输入框,输入电子邮箱
        time.sleep(2)
        e_mail = self.driver.find_element(By.ID, 'emailA')
        e_mail.send_keys('2951213431@qq.com')
        # 定位并点击注册按钮
        time.sleep(2)
        self.driver.find_element(By.CSS_SELECTOR, 'button').click()
        self.driver.switch_to.default_content()

    def exampleB(self):
        B = self.driver.find_element(By.NAME, 'myframe2')
        self.driver.switch_to.frame(B)
        # 定位账号输入框输入账号
        time.sleep(2)
        username = self.driver.find_element(By.ID, 'userB')
        username.send_keys('3333333')
        # 定位密码输入框输入密码
        time.sleep(2)
        password = self.driver.find_element(By.ID, 'passwordB')
        password.send_keys('12345678')
        # 定位电话号码输入框,输入电话号码
        time.sleep(2)
        tel = self.driver.find_element(By.ID, 'telB')
        tel.send_keys('13277968935')
        # 定位电子邮箱输入框,输入电子邮箱
        time.sleep(2)
        e_mail = self.driver.find_element(By.ID, 'emailB')
        e_mail.send_keys('2951213431@qq.com')
        # 定位并点击注册按钮
        time.sleep(2)
        self.driver.find_element(By.CSS_SELECTOR, 'button').click()

    def quit(self, second=5):
        time.sleep(second)
        self.driver.quit()

if __name__ == '__main__':

    register = Register()
    register.example()
    register.exampleA()
    register.exampleB()
    register.quit()
